import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 读取java文件输出到txt文件中（软著源代码使用）
 * @author magl
 */
public class ReadDirAndOut {

	private static final List<String> fileMap = new ArrayList<>();


	public static void main(String[] args) throws IOException {

//		String filePath = "D:\\Dev\\IdeaProjects\\comm-web\\src\\main\\webapp";
		String filePath = "D:\\Dev\\IdeaProjects\\op-expert-web-parent-v2\\op-expert-web\\src\\main\\java\\com\\zyt\\eb\\expert\\controller";
		getFiles(filePath);
		String outputPath = "d:/vue.txt";
		// 创建FileWriter写入b.txt文件
		FileWriter fw = new FileWriter(outputPath);
		try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath))) {
			for (String file : fileMap) {
				System.out.println(file);
				writer.newLine();
//				writer.write("-------------------------------------------");
				writer.write(file.replace("D:\\Dev\\IdeaProjects\\op-expert-web-parent-v2\\op-expert-web\\src\\main", "").replace("\\", "/"));
				writer.newLine();

				BufferedReader reader = new BufferedReader(new FileReader(file));
				String line;
				while ((line = reader.readLine()) != null) {

					if (!line.trim().startsWith("//")) {
						writer.write(line);
						writer.newLine();
					}
				}
				reader.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
//		for (String kv : fileMap) {
//			System.out.println(kv);
//
//			String inputPath = kv;
//
//
//
//
//			// 创建FileInputStream读取a.txt文件
//			FileInputStream fis = new FileInputStream(inputPath);
//			// 创建BufferedReader来读取文本
//			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//
//			// 创建BufferedWriter来写入文本
//			BufferedWriter bw = new BufferedWriter(fw);
//			bw.write(kv);
//			bw.newLine();
//			String line;
//			// 逐行读取a.txt的内容并写入b.txt
//			while ((line = br.readLine()) != null) {
//				if (!line.trim().startsWith("//")) {
//					bw.write(line);
//					bw.newLine(); // 写入一个行分隔符
//				}
//			}
//		}
	}

	/**
	 * 通过递归过得目录和文件
	 */
	public static void getFiles(String filePath) {

		File fileAll = new File(filePath);

		File[] files = fileAll.listFiles();

		for (File file : files) {
			if (file.isDirectory()) {
				//如果是目录
				//递归
				getFiles(file.getAbsolutePath());

//				fileList.add(file.getAbsolutePath());
//				System.out.println("显示" + filePath + "下所有子目录及其文件" + file.getAbsolutePath());
			} else {
//				if (!file.getAbsolutePath().contains("Controller.java")) {
//					continue;
//				}
				fileMap.add(file.getAbsolutePath());
			}

		}

	}

}